<?php
namespace Home\Controller;
use Think\Controller;

class MessageController extends Controller {
	
	public function msg_search(){

		$this -> display();
	}

	public function add_msg(){
		//接收信息ID和用户ID
		$user_id = session('user_id');

		
		if (isset($user_id)) {	

			if ($_GET['serve_id']) {
				$msg = $_COOKIE['msg_name'];
				$this -> assign('msg',$msg);
				$goods = D('Goods');
				$info = $goods -> find($_GET['serve_id']);
				$this -> assign('info',$info);
				$this -> assign('serve_id',$_GET['serve_id']);
				$this -> assign('s_user_id',$user_id);
				$this -> assign('style','none');
			}else{
				$this -> assign('style','block');
			}

			$goods_id = $_GET['goods_id']; 
			
			$this -> assign('goods_id',$goods_id);
			$this -> display();

		}else{

			$this -> redirect('User/login'); 
		}
				
	}

	
	public function add_need(){
		$user_id = session('user_id');

		$goods = D('Goods');

		$arr['user_id'] = $user_id;
		$arr['user_judge'] = 'serve';

		$info = $goods -> where($arr) ->select();

		$need_goods_id = $_GET['goods_id'];
		$this -> assign('need_goods_id',$need_goods_id);

		$this -> assign('info',$info);
		$this -> display();
	}

	//留言板
	public function msg_board(){

		$user_id = session('user_id');

		$goods = D('Goods');

		$arr['user_id'] = $user_id;
		$arr['user_judge'] = "need";
		$info = $goods -> where($arr) -> order('id desc') -> select();

		$arr = [];
		foreach ($info as $val){
			if($val['msg_num'] != ''){
				$arr[] = $val;
			}
		}
		// dump($info);
		$this -> assign('info',$arr);
		$this ->display();
	}

	//留言板详情
	public function msg_detail(){
		//获得商品id
		$need_id = $_GET['need_id'];
		

		$msg = D('Message');
		$user = D('User');
		$goods = D('Goods');

		$arr['need_msg_id'] = $need_id;

		$info = $msg -> where($arr) -> order('content_time desc')-> select();

		foreach ($info as $k => &$v) {
			$goodsInfo = $goods -> find($v['serve_msg_id']);
			$userInfo = $user -> find($goodsInfo['user_id']);
			$v['serve_name'] = $userInfo['user_name'];
			$v['tel'] = $goodsInfo['tel'];
			$v['goods_photo'] = $goodsInfo['goods_photo'];
			$v['conten_time'] = $this->panduan($v['conten_time']);
		}

		$this -> assign('info',$info);
		$this ->display();
	}

	function panduan($t){
	$ttime = strtotime('today');
	if($t >= $ttime){
		return date('H:i',$t);
	}else if($t >= $ttime - 86400 && $t < $ttime){
		return '昨天';
	}else{
		return date('Y-m-d',$t);
	}
}

	//编辑留言版
	public function my_msg_board(){

		$user_id = session('user_id');

		$goods = D('Goods');

		$arr['user_id'] = $user_id;
		$arr['user_judge'] = "need";
		$info = $goods -> where($arr) -> select();
		
		// dump($info);
		$this -> assign('info',$info);

		$this ->display();
	}

	// public function deleteMsg(){

	// }

	public function addMessage(){
				
		$arr = array(
			'message_content' =>$_POST['message_content'],
			'need_msg_id' =>$_POST['need_msg_id'],
			'serve_msg_id' =>$_POST['serve_msg_id'],
			'content_time' => date('ymdhis',time()),
			's_user_id' => $_POST['s_user_id']
			);

		$msg = D('Message');
		
		$re = $msg -> add($arr);
		if ($re) {
			$goods = D('Goods');
			$need_id = $_POST['need_msg_id'];
			$goodsInfo = $goods ->find($need_id);
			$goodsInfo['msg_num'] = $goodsInfo['msg_num']+1;

			$result = $goods -> save($goodsInfo);

			echo session('need_url');
		}else{
			echo "0";
		}
		
	}

}